package com.swz.dcrm.ui.home;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.CarModel;
import com.swz.dcrm.model.Clue;
import com.swz.dcrm.model.ClueRecord;
import com.swz.dcrm.model.Quotation;
import com.swz.dcrm.ui.SuccessFragmentArgs;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.Body;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.CarBrandViewModel;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.util.SingleClick;
import com.swz.dcrm.util.Tool;

import java.math.BigDecimal;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class QuotationFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_brand_name)
    TextView tvBrandName;
    @BindView(R.id.tv_model)
    TextView tvModel;

    @Inject
    CarManagementViewModel carManagementViewModel;
    @Inject
    CarBrandViewModel carBrandViewModel;

    @BindView(R.id.tv_car_price)
    TextView tvCarPrice;
    @BindView(R.id.tv_acquisition_tax)
    TextView tvAcquisitionTax;
    @BindView(R.id.tv_boutique_fee)
    TextView tvBoutiqueFee;
    @BindView(R.id.tv_insurance_fee)
    TextView tvInsuranceFee;
    @BindView(R.id.tv_loan_fee)
    TextView tvLoanFee;
    @BindView(R.id.tv_maintenance_fee)
    TextView tvMaintenanceFee;
    @BindView(R.id.tv_other_fee)
    TextView tvOtherFee;
    @BindView(R.id.tv_license_plate_fee)
    TextView tvLicensePlateFee;
    @BindView(R.id.tv_total_fee)
    TextView tvTotalFee;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_warning)
    TextView tvWarning;

    private Quotation quotation;
    private Clue clue;

    public static QuotationFragment newInstance() {

        return new QuotationFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        carBrandViewModel.getSelectedCarSeries().observe(this, carSeries -> {
            if (carSeries != null) {
                tvBrandName.setText(carBrandViewModel.getSelectedCarBrand().getValue().getName() +
                        carSeries.getName());
            }
        });
        carBrandViewModel.getSelectedCarModel().observe(this, carModel -> {
            if (carModel != null) {
                tvModel.setText(carModel.getName());
            }
        });

        QuotationFragmentArgs quotationFragmentArgs=QuotationFragmentArgs.fromBundle(getArguments());
        if(quotationFragmentArgs.getId().equals("test")){
            carManagementViewModel.getAddQuotation().observe(this,quotationBaseResponse -> {
                if(quotationBaseResponse.isSuccess()){
                    quotation=quotationBaseResponse.getData();
                    initData();
                    if(quotation.isNeedApproval()){
                        tvConfirm.setText(getString(R.string.feel_in_total_needApproval));
                        tvWarning.setVisibility(View.VISIBLE);
                    }else {
                        tvConfirm.setText(getString(R.string.feel_in_total_create_make_sure));
                        tvWarning.setVisibility(View.GONE);
                    }
                }
            });
        }else {
            carManagementViewModel.getQuotationById(Long.valueOf(quotationFragmentArgs.getId()))
                    .observe(this,quotationBaseResponse -> {
                        quotation=quotationBaseResponse.getData();
                        initData();
                        tvModel.setText(quotation.getCarModelName());
                        tvBrandName.setText(quotation.getCarSeriesName());

                        tvConfirm.setVisibility(View.GONE);
                        tvWarning.setVisibility(View.GONE);

                    });
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
    @Override
    public int layoutId() {
        return R.layout.fragment_quotation;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.feel_in_total_quotation));
        onLoadRetry();
        return true;
    }

    private void initData(){
        carManagementViewModel.getCurrentSelectedClueRecord().observe(this, clue -> {
            if(clue!=null){
                carManagementViewModel.getCurrentSelectedClueRecord().setValue(null);
                this.clue=clue;
                confirm();

            }
        });
        tvCarPrice.setText(Tool.formatToNumber(new BigDecimal(quotation.getCarPrice())));
        tvAcquisitionTax.setText(Tool.formatToNumber(new BigDecimal(quotation.getAcquisitionTax())));
        tvLoanFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getLoanFee())));
        tvLicensePlateFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getLicensePlateFee())));
        tvMaintenanceFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getMadoubleenanceFee())));
        tvOtherFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getOtherFee())));
        tvBoutiqueFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getBoutiquePrice())));
        tvInsuranceFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getInsurancePrice())));
        tvTotalFee.setText(Tool.formatToNumber(new BigDecimal(quotation.getNormalCarFullPrice())));

    }

    @OnClick(R.id.tv_confirm)
    @SingleClick
    public void confirm(){
        if(quotation.isNeedApproval()){
            if(clue==null){
                go(null,R.id.action_quotationFragment_to_clueSelectFragment,null);
                return;
            }
                carManagementViewModel.applyApproval(quotation.getId(),clue.getId())
                        .observe(this, stringBaseResponse -> {
                            showToast(stringBaseResponse.getMsg());
                            if(stringBaseResponse.isSuccess()){
                                SuccessFragmentArgs.Builder builder=new SuccessFragmentArgs.Builder();
                                builder.setContent(getString(R.string.feel_in_total_approval_success));
                                go(null,R.id.action_quotationFragment_to_SuccessFragment,builder.build().toBundle());
                            }
                        });
        }else {
            AddOrderFragmentArgs.Builder builder=new AddOrderFragmentArgs.Builder();
            builder.setQuotation(new Gson().toJson(quotation));
            go(null,R.id.action_quotationFragment_to_AddOrderFragment,builder.build().toBundle());
        }
    }
    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
